using UnityEngine;
using System.Collections;
using System.Linq;

public class FireCoin : MonoBehaviour
{

    public GameObject coinPrefab;
    public float force = 16.0f;

	// Use this for initialization
	void Start ()
	{
	    StartCoroutine(FireOne());
	}

    private bool firing = false;
    private IEnumerator FireOne()
    {
        while (true)
        {
            if(firing)
            {
                var ball = (Instantiate(coinPrefab) as GameObject).GetComponent<Rigidbody>();
                ball.useGravity = true;
                ball.transform.position = this.transform.position;
                ball.AddForce(ball.transform.forward*force, ForceMode.Impulse);
                ball.gameObject.AddComponent<SafeDelete>();
            }
            yield return new WaitForSeconds(0.1f);
        }
    }

    // Update is called once per frame
	void Update () {
        firing = Input.GetKey(KeyCode.Space);
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var wheels = GameObject.FindObjectsOfType(typeof (LuckWheel));
            foreach (LuckWheel wheel in wheels)
            {
                wheel.Go();
            }
        }
	}
}
